
package com.bluelinelabs.conductor.demo.controllers;

import com.bluelinelabs.conductor.conductor.*;
import com.bluelinelabs.conductor.conductor.changehandler.FadeChangeHandler;
import com.bluelinelabs.conductor.conductor.manager.ObjectManager;
import com.bluelinelabs.conductor.demo.ResourceTable;
import com.bluelinelabs.conductor.demo.controllers.base.BaseController;
import com.bluelinelabs.conductor.demo.util.ColorUtil;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

import java.util.List;

/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class ParentController extends BaseController {
    private boolean hasShownAll = false;
    private boolean animatingOut = false;
    public static final int NUMBER_OF_CHILDREN = 5;
    private Component binding;


    public ParentController() {
        super(ResourceTable.Layout_controller_parent);

    }

    @Override
    public void onViewCreated(Component component) {
        title = "Parent/Child Demo";
        ObjectManager.getInstance().setIdTitleAboutSetVisibility(Component.HIDE);
        binding = component;
    }

    @Override
    protected void onChangeEnded(ControllerChangeHandler changeHandler, ControllerChangeType changeType) {
        if (changeType == ControllerChangeType.PUSH_ENTER) {
            addChild(0);
        }
    }

    private void addChild(int index) {
        ComponentContainer container = null;
        switch (index) {
            case 0:
                container = (ComponentContainer) binding.findComponentById(ResourceTable.Id_child_content_1);
                break;
            case 1:
                container = (ComponentContainer) binding.findComponentById(ResourceTable.Id_child_content_2);
                break;
            case 2:
                container = (ComponentContainer) binding.findComponentById(ResourceTable.Id_child_content_3);
                break;
            case 3:
                container = (ComponentContainer) binding.findComponentById(ResourceTable.Id_child_content_4);
                break;
            case 4:
                container = (ComponentContainer) binding.findComponentById(ResourceTable.Id_child_content_5);
                break;
            default:
                System.out.println("Invalid child index $index");
                return;
        }

        Router childRouter = getChildRouter(container).setPopsLastView(true);
        if (!childRouter.hasRootController()) {
            IntentParams intentParams = new IntentParams();
            intentParams.setParam(ChildController.KEY_TITLE, "Child Controller #" + index);
            intentParams.setParam(ChildController.KEY_BG_COLOR, ColorUtil.getMaterialColor(binding.getContext(), index));
            intentParams.setParam(ChildController.KEY_COLOR_IS_RES, false);
            ChildController child = new ChildController(intentParams);

            child.addLifecycleListener(new LifecycleListener() {
                @Override
                public void onChangeEnd(Controller controller, ControllerChangeHandler changeHandler, ControllerChangeType changeType) {
                    if (!isBeingDestroyed()) {
                        if (changeType == ControllerChangeType.PUSH_ENTER && !hasShownAll) {
                            if (index < NUMBER_OF_CHILDREN - 1) {
                                addChild(index + 1);
                            } else {
                                hasShownAll = true;
                            }
                        } else if (changeType == ControllerChangeType.POP_EXIT) {
                            if (index > 0) {
                                removeChild(index - 1);
                            } else {
                                getRouter().popController(ParentController.this);
                            }
                        }
                    }
                }
            });


            childRouter.setRoot(
                    RouterTransaction.with(child)
                            .pushChangeHandler(new FadeChangeHandler())
                            .popChangeHandler(new FadeChangeHandler())
            );
        }
    }


    @Override
    public boolean handleBack() {
        int childControllers = 0;
        List<Router> childRouters = getChildRouters();
        for (Router info : childRouters) {
            if (info.hasRootController()) {
                childControllers += 1;
            }
        }
        if (childControllers != NUMBER_OF_CHILDREN || animatingOut) {
            return true;
        } else {
            animatingOut = true;
            return super.handleBack();
        }

    }

    private void removeChild(int index) {
        List<Router> childRouters = getChildRouters();
        if (index < childRouters.size()) {
            removeChildRouter(childRouters.get(index));
        }
    }
}
